package com.schoolvisitors.controller.admin;

import com.schoolvisitors.context.AdminContext;
import com.schoolvisitors.dto.AdminLoginDto;
import com.schoolvisitors.entity.Admin;
import com.schoolvisitors.response.ResultBody;
import com.schoolvisitors.service.AdminService;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/admin")
public class AdminAuthController {
    private final AdminService adminService;

    public AdminAuthController(AdminService adminService) {
        this.adminService = adminService;
    }

    @PostMapping("/login")
    public ResultBody login(@RequestBody @Valid AdminLoginDto adminLoginDto){
        return ResultBody.success(adminService.login(adminLoginDto));
    }

    @PostMapping("/logout")
    public ResultBody logout(@RequestHeader("Authorization") String token){
        Admin admin = AdminContext.get();
        adminService.logout(admin, token);
        return ResultBody.success();
    }
}
